<?php
namespace app\home\controller;
use think\Controller;
use think\Validate;
class Home extends Base
{
    //注册页面
    public function register()
    {
        if($this->request->isPost())
        {
            //dump(input());
            $result=$this->validate(input(),'Member');
            if($result===true)
            {
                $time=time();
                $arr=[
                    'username'=>input('post.username'),
                    'password'=>input('post.pwd'),
                    'name'=>input('post.name'),
                    'phone'=>input('post.phone'),
                    'email'=>input('post.email'),
                    'time'=>$time,
                ];
                model('Member')->addm($arr);
                return $this->success('注册成功');
            }
            else
            {
                return $this->error($result);
            }
        }
        else
        {
            return $this->fetch();
        } 
    }
    //登录页面
    public function login()
    {
        if($this->request->isPost())
        {
            //dump(input());
            $res=model('Member')->checkLogin();
            if($res['code']==1)
            {
                return $this->success($res['msg']);
            }
            else
            {
                return $this->error($res['msg']);
            }
        }
        else
        {
            return $this->fetch();
        }    
    }
    //退出登录
    public function outLogin()
    {
        session('qid',null);
        return $this->success('退出成功','home/Home/index');
    }

    //前台主页
    public function index()
    {

        //dump(session('qid'));
        
        $res=model('newsContent')->sel3();
        //主页宝典部分展示
        $this->assign('res',$res);
        $a='';
        foreach($res as $v)
        {
            $a=$v['allimg'];
        }
        $a=explode(',',$a);
        $a=$a[0];
        //dump($a);
        //宝典图片展示
        $this->assign('a',$a);

        $fl=model('friendLink')->sel();
        //友情链接展示
        $this->assign('fl',$fl);
        
        $img=model('ad')->sel4();
        //dump($img);
        $la='';
        foreach($img as $v)
        {
            $v['address']=str_replace("\\","/",$v['address']);
        }
        //$la=substr($la,0,-1);
        //dump($img);
        //广告展示
        $this->assign('img',$img);

        $bfirst = model('Brand')->field("first")->distinct( true )->order(['first'])->select();
        $brand = model('Brand')->where("is_show",1)->order(['first','sort'=>'desc'])->select();
        $level = db("Level")->where("is_show",1)->order(['sort'=>'desc'])->select();
        $condition = [];
      	if(session('area') != null){
            $condition []= ["a.name","=",session('area')];
        }
        $condition []= ["s.is_show","=",1];
        $source = db("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_cate c","t.cid=c.id")
          	->join("tp_area a","a.id=s.aid")
            ->where($condition)
            ->order(['pubtime'])
            ->limit(8)
            ->select();
      	$condition []= ['s.is_commend',"=",1];
        $recommed = db("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_cate c","t.cid=c.id")
          	->join("tp_area a","a.id=s.aid")
            ->where($condition)
            ->order(['pubtime'])
            ->limit(8)
            ->select();
        $this->assign('recommed',$recommed);
        $this->assign('source',$source);
        $this->assign('brand',$brand);
        $this->assign('bfirst',$bfirst);
        $this->assign('level',$level);
        return $this->fetch();
    }

    /**
     * 买车搜索页
     */
    public function buycar(){
        if($this->request->isGet()){
            //单个清除
            if(input('del')){
                session(input('del'),null);
                if(input('del')=='bname'){
                    session('first',null);
                }
                if(input('del')=='all'){
                    session('first',null);
                    session('bname',null);
                    session('lname',null);
                    session('preprice',null);
                    session('distance',null);
                    session('emst',null);
                    session('gas',null);
                    session('transmission',null);
                }
            }
            //排放标准
            if(input("emst")){
                $condition []= ["t.emst","=",input("emst")];
                session('emst',input("emst"));
            }elseif(session("emst")){
                $condition []= ["t.emst","=",session("emst")];
            }
            //排量
            if(input("gas")){
                if(input("gas")==1){
                    $condition []= ["t.cc","<=",1];
                }elseif(input("gas")==2){
                    $condition []= ["t.cc",">",1];
                    $condition []= ["t.cc","<=",2];
                }elseif(input("gas")==3){
                    $condition []= ["t.cc",">",2];
                    $condition []= ["t.cc","<=",2.5];
                }elseif(input("gas")==4){
                    $condition []= ["t.cc",">",2.5];
                    $condition []= ["t.cc","<=",3];
                }elseif(input("gas")==5){
                    $condition []= ["t.cc",">",3];
                    $condition []= ["t.cc","<=",4];
                }elseif(input("gas")==6){
                     $condition []= ["t.cc",">",4];
                }
                session('gas',input("gas"));
            }elseif(session("gas")){
                if(session("gas")==1){
                    $condition []= ["t.cc","<=",1];
                }elseif(session("gas")==2){
                    $condition []= ["t.cc",">",1];
                    $condition []= ["t.cc","<=",2];
                }elseif(session("gas")==3){
                    $condition []= ["t.cc",">",2];
                    $condition []= ["t.cc","<=",2.5];
                }elseif(session("gas")==4){
                    $condition []= ["t.cc",">",2.5];
                    $condition []= ["t.cc","<=",3];
                }elseif(session("gas")==5){
                    $condition []= ["t.cc",">",3];
                    $condition []= ["t.cc","<=",4];
                }elseif(session("gas")==6){
                    $condition []= ["t.cc",">",4];
                }
            }
            //变速器  
            if(input("transmission")){
                $condition []= ["t.transmission","like","%".input("transmission")."%"];
                session('transmission',input("transmission"));
            }elseif(session("transmission")){
                $condition []= ["t.transmission","like","%".session("transmission")."%"];
            }
            //品牌
            if(input("bname")){
                $condition []= ["b.bname","=",input("bname")];
                session('bname',input("bname"));
                session('first',ucfirst(pinyin(input("bname"),'one')));
            }elseif(session("bname")){
                 $condition []= ["b.bname","=",session("bname")];
            }
            //车系
            if(input("keyword")){
                $condition []= ["t.tname","like","%".input("keyword")."%"];
            }
            //车级
            if(input("lname")){
                $condition []= ["l.lname","=",input("lname")];
                session('lname',input("lname"));
            }elseif(session("lname")){
                 $condition []= ["l.lname","=",session("lname")];
            }
            //价格
            if(input('preprice')){
                if(input('preprice')==3 || input('preprice')==5 || input('preprice')==10 || input('preprice')==20 || input('preprice')==30){
                    $condition []= ["s.preprice","<=",input("preprice")];

                }elseif(input('preprice')== ">30"){
                    $condition []= ["s.preprice",">",30];
                }
                session('preprice',input("preprice"));
            }elseif(session("preprice")){
                if(session('preprice')==3 || session('preprice')==5 || session('preprice')==10 || session('preprice')==20 || session('preprice')==30){
                    $condition []= ["s.preprice","<=",session("preprice")];
                }elseif(session('preprice') == ">30"){
                    $condition []= ["s.preprice",">",30];
                }
            }
            //里程
            if(input('distance')){
                if(input('distance')==1 || input('distance')==3 || input('distance')==5 || input('distance')==8 ){
                    $condition []= ["s.distance","<=",input("distance")];

                }elseif(input('distance')== "9"){
                    $condition []= ["s.distance",">",8];
                }
                session('distance',input("distance"));
            }elseif(session("distance")){
                if(session('distance')==1 || session('distance')==3 || session('distance')==5 || session('distance')==8){
                    $condition []= ["s.distance","<=",session("distance")];
                }elseif(session('distance') == "9"){
                    $condition []= ["s.distance",">",8];
                }
            }
        }
        if(session('area') != null){
            $condition []= ["a.name","=",session('area')];
        }
        $condition []= ["s.is_show","=",1];
        $bfirst = model('Brand')->field("first")->distinct( true )->order(['first'])->select();
        $brand = model('Brand')->where("is_show",1)->order(['first'])->select();
        $level = db("Level")->where("is_show",1)->order(['sort'=>'desc'])->select();
        $source = model("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_cate c","t.cid=c.id")
            ->join("tp_brand b","b.bid=c.bid")
            ->join("tp_level l","l.lid=t.lid")
            ->join("tp_area a","a.id=s.aid")
            ->where($condition)
            ->order(['pubtime'])
            ->paginate([
                'list_rows'=>24,
                'query'=>input('param.'),
            ]);
        $num = db("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_cate c","t.cid=c.id")
            ->join("tp_brand b","b.bid=c.bid")
            ->join("tp_level l","l.lid=t.lid")
            ->join("tp_area a","a.id=s.aid")
            ->where($condition)
            ->order(['pubtime'])
            ->count();
        $this->assign('num',$num);
        $this->assign('source',$source);
        $this->assign('brand',$brand);
        $this->assign('bfirst',$bfirst);
        $this->assign('level',$level);
        return $this->fetch();
    }
    
    //汽车详情页
    public function cardetail(){
        $source = db("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_level l","l.lid=t.lid")
            ->join("tp_cate c","t.cid=c.id")
            ->join("tp_brand b","b.bid=c.bid")
            ->join("tp_area a","a.id=s.aid")
            ->where('sid',input('sid'))
            ->select();
        if(session('area') != null){
            $condition []= ["a.name","=",session('area')];
        }
        $condition[]= ["s.is_show",'=',1];
        $condition []=['s.is_commend','=',1];
        $recommed = db("carsource")
            ->alias("s")
            ->join("tp_sourceimg m","m.mid=s.mid")
            ->join("tp_cartype t","t.tid=s.tid")
            ->join("tp_cate c","t.cid=c.id")
            ->join("tp_area a","a.id=s.aid")
            ->where($condition)
            ->order(['pubtime'])
            ->limit(8)
            ->select();
        
        $mid = db("carsource")->field('mid')->where('sid',input('sid'))->select();
        
        $img = db("sourceimg")->where('mid','in',$mid[0]['mid'])->select();
        $this->assign('recommed',$recommed);
        $this->assign('img',$img);
        $this->assign('info',$source);
        return $this->fetch();
    }

    /**
     * 全国地区默认
     */
    public function area(){
        if(input('id')!='quanguo'){
            $res = db('area')->where('id',input('id'))->select();
            session('area',$res[0]['name']);
        }else{
            session('area',null);
        }
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * 卖车
     */
    public function sellcar(){
        if($this->request->isPost()){
            if(input('post.cid')==null || input('post.tid')==null){
                return $this->error('请选择车型车系');
            }

            $res = db('evaluate')->insert([
                'aid'=>input('post.aid'),
                'tid'=>input('post.tid'),
                'oname'=>session('username'),
                'ophone'=>input('post.ophone'),
            ]);
            if($res>0){
                return $this->success('提交成功');
            }else{
                return $this->error('提交失败');
            }
        }else{
        //地区
            $area = db('area')->where('level',1)->order(['first'])->select();
            if(input("get.pid") != null){
                $area2 = db('area')->where('fid',input("get.pid"))->order(['first'])->select();
                return $area2;
            }else{
                $area2 = db('area')->where('fid',$area[0]['id'])->order(['first'])->select();
            }
            //品牌
            $brand = model('Brand')->order(['first'])->select();
            if(input("get.bid") != null){
                $c['cate'] = db('cate')->where('bid',input("get.bid"))->select();

                $num = db('cate')->where('bid',input("get.bid"))->count();

                $c['gname'] = db('cate')->field('gname')->distinct ( true )->where('bid',input("get.bid"))->select();
                if($num>0){
                    $c['type'] = db("Cartype")->where('cid',$c['cate'][0]['id'])->select();
                }else{
                    $c['type'] = [];
                }
                
                return $c;
            }
            if(input("get.cid") != null){
                $cartype = db('cartype')->where('cid',input("get.cid"))->select();
                return $cartype;
            }
            $this->assign('brand',$brand);
            $this->assign('area',$area);
            $this->assign('area2',$area2);
            return $this->fetch();
        }   
    }
}
